global.config = require('config').values;
var io      = require('socket.io').listen(config.im.port);
            console.log('listen:'+config.im.port);
global.baseCode = require('baseCode')(io);

config.error = require('log')();
var roomManager = require('roomManager');
    roomManager = new roomManager(io);
var userMgr = require('userMgr');
    userMgr = new userMgr(io);

//begin
io.sockets.on('connection', function(socket){
    try{
        //验证用户、用户昵称、用户数据相关处理
        userMgr.connect(socket);

        //init
        roomManager.init(socket);

        config.error(socket, {action:'connection'});
        //var historyData = getHistoryData(socket.handshake.headers.cookie);
        socket.emit('llUserLoadIn', {userData:getUserData(socket), history:[], roomData:config.customerData[socket.roomId]});
        broadcast(socket, 'llUserIn', socket.userData.nick + ' is come in.');

        //获取数据
        socket.on('get_draws', function(msg){
            try{
                //do:
                this.emit('get_draws', roomManager.getData(this));

                //return:
            }catch(err){
                config.error(this, {action:'get_draws', data:err});
                this.emit('llCallback', 'get_draws', false, msg);
            }
        });

        //通讯
        socket.on('chat', function(msg){
            try{
                //do:
                broadcast(this, 'llChat', msg);

                //return:
                this.emit('llCallback', 'llChat', true, msg);
            }catch(err){
                config.error(this, {action:'chat', data:err});
                this.emit('llCallback', 'llChat', false, msg);
            }
        });

        //通讯
        socket.on('draw', function(msg){
            try{
                //do:
                roomManager.userAction(this, msg);
                broadcast(this, 'draw', msg);
            }catch(err){
                config.error(this, {action:'draw', data:err});
                this.emit('llCallback', 'draw', false, msg);
            }
        });

        //获取列表
        socket.on('getUserListByRoomId', function(roomId){
            try{
                //do:
                var roomList = io.sockets.adapter.rooms[socket.roomName];
                var data = [];
                for(var k in roomList){
                    if(roomList.hasOwnProperty(k)){
                        data.push(io.sockets.connected[k].userData);
                    }
                }
                //return:
                this.emit('llCallback', 'getUserListByRoomId', true, data);
            }catch(err){
                config.error(this, {action:'getUserListByRoomId', data:err});
                this.emit('llCallback', 'getUserListByRoomId', false, roomId);
            }
        });
        //离开
        socket.on('disconnect', function(){
            try{
                userMgr.disconnect(socket);
                roomManager.disconnect(this);
                broadcast(this, 'llUserOut', this.userData.nick + ' is come out.');
            }catch(err){
                config.error(this, {action:'disconnect', data:err});
                console.log(err);
            }
        });
        function broadcast(client, eventName, data){
            //console.info(client.userData.nick+'::roomBroadcast ---- ' + eventName + ' ----- ' + client.roomName);
            client.broadcast.to(client.roomName).emit(eventName, getUserData(client), data);
        }

        function getUserData(client){
            return client.userData;
        }
        function log(client, str){
            //console.log('-user: '+client.userData.nick+'  ---:'+str);
        }
    }catch(err){
        console.log(err);
        config.error(this, {action:'connection', data:err});
        return 0;
    }
});


function logUser(client){
    console.log('________________room-users:');
    var roomList = io.sockets.adapter.rooms[client.roomName];
    for(var k in roomList){
        if(roomList.hasOwnProperty(k)){
            console.log(baseCode.getClientById(k).userData.nick);
        }
    }
}
function getHistoryData(cookie){
    var room = getCookieValue(cookie, 'XroomData');
    if(room){
        room = room.split(',');
    }else{
        room = [];
    }
    return room;
}